package org.formal.system.controller;

import org.formal.common.controller.BaseController;
import org.formal.common.data.ResultData;
import org.formal.system.entity.UserEntity;
import org.formal.system.entity.UserRoleEntity;
import org.formal.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户控制类
 * @author jiangyan
 *
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController<UserEntity> {
	
	@Autowired
	private UserService userService;
		
	/**
	 * 包含用户及角色信息
	 */
	@PostMapping(value="/save/roles")
	public ResultData saveUserWithRoles(UserEntity entity) {
		ResultData data;
		try {
			this.userService.saveUserWithRoles(entity);
			data = getResultData(true, "保存用户成功");
		} catch (Exception e) {
			data = getResultData(false, "保存用户失败");
		}
		return data;
	}

	/**
	 * 获取用户默认关联角色
	 * @param userId 用户ID
	 * @return 返回用户角色关联对象
	 */
	@GetMapping(value="getDefaultRoleId")
	public UserRoleEntity getDefaultRoleId(String userId) {
		return this.userService.getDefaultRole(userId);
	}
}
